package com.demo.villageresidence.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.dto.VehicleDTO;
import com.demo.villageresidence.pojo.User;
import com.demo.villageresidence.pojo.VehicleApply;
import com.demo.villageresidence.pojo.VehicleApproval;
import com.demo.villageresidence.service.UserService;
import com.demo.villageresidence.service.VehicleApplyService;
import com.demo.villageresidence.service.VehicleApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/vehicleApproval")
public class VehicleApprovalController {

    @Autowired
    private VehicleApprovalService vehicleApprovalService;

    @Autowired
    private VehicleApplyService vehicleApplyService;

    @Autowired
    private UserService userService;

    @GetMapping("getVehicleApplyByPagePass")
    public Result getVehicleApplyByPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<VehicleApproval> page = vehicleApprovalService.query()
                .page(new Page<>(current, pageSize));
        // 获取当前页数据
        List<VehicleApproval> VehicleApprovalList = page.getRecords();

        List<VehicleDTO> vehicleDTOList = new ArrayList<>();

        VehicleApprovalList.forEach(vehicleApproval -> {
            VehicleDTO vehicleDTO = SetVehicleDTO(vehicleApproval);
            vehicleDTOList.add(vehicleDTO);
        });

        return Result.ok(vehicleDTOList, page.getTotal());
    }

    private VehicleDTO SetVehicleDTO(VehicleApproval vehicleApproval) {
        VehicleApply vehicleApply = vehicleApplyService.getById(vehicleApproval.getVeApplyId());
        VehicleDTO vehicleDTO = new VehicleDTO();
        vehicleDTO.setVeApplyId(vehicleApply.getVeApplyId());
        vehicleDTO.setUserPhone(userService.getById(vehicleApply.getUserId()).getUserPhone());
        vehicleDTO.setVeName(vehicleApply.getVeName());
        vehicleDTO.setVeType(vehicleApply.getVeType());
        vehicleDTO.setVeNumber(vehicleApply.getVeNumber());
        vehicleDTO.setVeTime(vehicleApply.getVeTime());
        vehicleDTO.setVeStatus(vehicleApply.getVeStatus());
        vehicleDTO.setUserId(vehicleApply.getUserId());
        vehicleDTO.setApproStatus(vehicleApproval.getApproStatus());
        vehicleDTO.setApproTime(vehicleApproval.getApproTime());
        vehicleDTO.setApproNote(vehicleApproval.getApproNote());
        vehicleDTO.setAdminId(vehicleApproval.getAdminId());
        vehicleDTO.setIsDelete(vehicleApproval.getIsDelete());
        vehicleDTO.setVeApproId(vehicleApproval.getVeApproId());
        return vehicleDTO;
    }

    @GetMapping("getVehicleOneByVeApproId/{id}")
    public Result getVehicleOneByVeApproId(@PathVariable("id") Integer veApproId) {

        VehicleApproval vehicleApproval = vehicleApprovalService.getById(veApproId);
        VehicleDTO vehicleDTO = SetVehicleDTO(vehicleApproval);
        return Result.ok(vehicleDTO);
    }


    @PostMapping("addVehicleApproval")
    public Result addVehicleApproval(@RequestBody VehicleApproval vehicleApproval) {


        // 设置逻辑删除状态
        vehicleApproval.setIsDelete(0);

        // 设置审批时间
        vehicleApproval.setApproTime(LocalDateTime.now());
        if (vehicleApprovalService.save(vehicleApproval)) {
            // 修改申请表的审批状态
            vehicleApplyService.update().eq("ve_apply_id", vehicleApproval.getVeApplyId()).set("ve_status", 1).update();
            return Result.ok("审批成功");
        } else {
            return Result.fail("审批失败");
        }
    }

    @GetMapping("getApprovalByVeApplyId/{veApplyId}")
    public Result getApprovalByVeApplyId(@PathVariable("veApplyId") Integer veApplyId) {
        VehicleApproval vehicleApproval = vehicleApprovalService.query().eq("ve_apply_id", veApplyId).one();
        VehicleDTO vehicleDTO;
        if (vehicleApproval == null) {
            vehicleDTO = new VehicleDTO();
            vehicleDTO.setVeApplyId(veApplyId);
            vehicleDTO.setUserPhone(userService.getById(vehicleApplyService.getById(veApplyId)).getUserPhone());
            vehicleDTO.setVeName(vehicleApplyService.getById(veApplyId).getVeName());
            vehicleDTO.setVeType(vehicleApplyService.getById(veApplyId).getVeType());
            vehicleDTO.setVeNumber(vehicleApplyService.getById(veApplyId).getVeNumber());
            vehicleDTO.setVeTime(vehicleApplyService.getById(veApplyId).getVeTime());
            vehicleDTO.setVeStatus(vehicleApplyService.getById(veApplyId).getVeStatus());
            vehicleDTO.setUserId(vehicleApplyService.getById(veApplyId).getUserId());
            vehicleDTO.setApproNote("");
            vehicleDTO.setAdminId(0);
            vehicleDTO.setIsDelete(0);
            vehicleDTO.setVeApproId(-1);
        }else {
            vehicleDTO = SetVehicleDTO(vehicleApproval);
        }
        return Result.ok(vehicleDTO);

    }



}
